```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring事务管理深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-title {
            font-family: 'Noto Serif SC', serif;
        }
        .code-block {
            background-color: #f8f9fa;
            border-left: 4px solid #3b82f6;
            padding: 1rem;
            overflow-x: auto;
        }
        .code-keyword {
            color: #d63384;
        }
        .code-class {
            color: #6f42c1;
        }
        .code-method {
            color: #0d6efd;
        }
        .code-string {
            color: #20c997;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .transition-all {
            transition: all 0.3s ease;
        }
        .first-letter:first-letter {
            float: left;
            font-size: 3em;
            line-height: 0.9;
            margin-right: 0.2em;
            color: #3b82f6;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <div class="relative bg-gradient-to-r from-blue-500 to-indigo-600 py-20 px-4 sm:px-6 lg:px-8 text-white">
        <div class="max-w-7xl mx-auto">
            <div class="text-center">
                <h1 class="hero-title text-4xl md:text-5xl font-bold mb-4">Spring事务管理深度解析</h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">探索Spring如何保障数据一致性与完整性的核心技术</p>
                <div class="inline-flex items-center space-x-2 bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-book-open"></i>
                    <span>技术小馆出品</span>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0 h-16 bg-gradient-to-t from-gray-50 to-transparent"></div>
    </div>

    <!-- Main Content -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <div class="mb-16">
            <p class="first-letter text-lg md:text-xl text-gray-700 leading-relaxed">
                事务管理是Spring框架的核心功能之一，通过高效的事务管理机制，Spring保证了企业级应用中的数据一致性、完整性和可靠性。无论是简单的单数据库操作，还是复杂的分布式事务场景，Spring都提供了完善的解决方案。本文将深入剖析Spring事务管理的实现原理，揭示其背后的设计哲学与实现细节。
            </p>
        </div>

        <!-- Core Components -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold mr-4">1</div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800">Spring事务管理的核心组件</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-xl shadow-md transition-all card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center mr-4">
                            <i class="fas fa-cogs"></i>
                        </div>
                        <h3 class="text-xl font-semibold">PlatformTransactionManager</h3>
                    </div>
                    <p class="text-gray-600">事务管理器接口，定义了事务管理的基本操作，如开始、提交和回滚事务。作为Spring事务抽象的核心，它隔离了不同事务API(JDBC,JPA等)的具体实现。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md transition-all card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-4">
                            <i class="fas fa-file-contract"></i>
                        </div>
                        <h3 class="text-xl font-semibold">TransactionDefinition</h3>
                    </div>
                    <p class="text-gray-600">事务的定义接口，封装了事务的隔离级别、传播行为、超时时间等属性。它提供了配置事务行为的统一方式，无论是编程式还是声明式事务。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md transition-all card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-green-100 text-green-600 flex items-center justify-center mr-4">
                            <i class="fas fa-info-circle"></i>
                        </div>
                        <h3 class="text-xl font-semibold">TransactionStatus</h3>
                    </div>
                    <p class="text-gray-600">事务状态对象，记录当前事务的运行状态信息。通过它可以查询事务是否已完成、是否标记为回滚等状态，也能触发事务回滚操作。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md transition-all card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-red-100 text-red-600 flex items-center justify-center mr-4">
                            <i class="fas fa-exchange-alt"></i>
                        </div>
                        <h3 class="text-xl font-semibold">TransactionInterceptor</h3>
                    </div>
                    <p class="text-gray-600">事务拦截器，是声明式事务的关键实现。通过AOP机制拦截方法调用，在方法执行前后处理事务的开始、提交和回滚等操作。</p>
                </div>
            </div>
        </section>

        <!-- Transaction Modes -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold mr-4">2</div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800">Spring事务管理模式</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-xl shadow-md transition-all card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 text-indigo-600 flex items-center justify-center mr-4">
                            <i class="fas fa-code"></i>
                        </div>
                        <h3 class="text-xl font-semibold">编程式事务管理</h3>
                    </div>
                    <p class="text-gray-600 mb-4">开发人员通过API显式控制事务的生命周期，需要手动管理事务的开始、提交和回滚。提供了更细粒度的控制，但代码侵入性较强。</p>
                    <div class="code-block">
                        <pre><code>TransactionStatus status = transactionManager.getTransaction(definition);
try {
    // 业务逻辑
    transactionManager.commit(status);
} catch (Exception e) {
    transactionManager.rollback(status);
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md transition-all card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-teal-100 text-teal-600 flex items-center justify-center mr-4">
                            <i class="fas fa-magic"></i>
                        </div>
                        <h3 class="text-xl font-semibold">声明式事务管理</h3>
                    </div>
                    <p class="text-gray-600 mb-4">通过注解或XML配置来管理事务，Spring自动处理事务的生命周期。减少了样板代码，保持了业务逻辑的纯粹性，是最常用的方式。</p>
                    <div class="code-block">
                        <pre><code>@Transactional
public void businessMethod() {
    // 业务逻辑
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Declarative Transaction -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold mr-4">3</div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800">声明式事务的实现机制</h2>
            </div>
            
            <div class="mb-12">
                <h3 class="text-xl font-semibold mb-4 flex items-center">
                    <span class="w-6 h-6 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center text-sm mr-3">3.1</span>
                    @Transactional注解解析
                </h3>
                <p class="text-gray-700 mb-6">当在方法或类上使用@Transactional注解时，Spring通过AOP代理机制自动为该方法创建事务代理。注解的属性定义了事务的具体行为。</p>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                    <div class="bg-gray-800 text-white px-4 py-2 flex items-center">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                        <span class="text-sm ml-2">Transactional.java</span>
                    </div>
                    <div class="code-block">
                        <pre><code><span class="code-keyword">@Target</span>({<span class="code-class">ElementType</span>.METHOD, <span class="code-class">ElementType</span>.TYPE})
<span class="code-keyword">@Retention</span>(<span class="code-class">RetentionPolicy</span>.RUNTIME)
<span class="code-keyword">public</span> <span class="code-keyword">@interface</span> <span class="code-class">Transactional</span> {
    <span class="code-class">String</span> <span class="code-method">value</span>() <span class="code-keyword">default</span> <span class="code-string">""</span>;
    <span class="code-class">Propagation</span> <span class="code-method">propagation</span>() <span class="code-keyword">default</span> <span class="code-class">Propagation</span>.REQUIRED;
    <span class="code-class">Isolation</span> <span class="code-method">isolation</span>() <span class="code-keyword">default</span> <span class="code-class">Isolation</span>.DEFAULT;
    <span class="code-keyword">int</span> <span class="code-method">timeout</span>() <span class="code-keyword">default</span> -1;
    <span class="code-keyword">boolean</span> <span class="code-method">readOnly</span>() <span class="code-keyword">default</span> <span class="code-keyword">false</span>;
    <span class="code-class">String</span> <span class="code-method">rollbackFor</span>() <span class="code-keyword">default</span> <span class="code-string">""</span>;
}</code></pre>
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-blue-50 p-4 rounded-lg border border-blue-100">
                        <h4 class="font-medium text-blue-800 mb-2">关键属性说明</h4>
                        <ul class="text-sm text-gray-700 space-y-2">
                            <li><span class="font-medium">propagation</span> - 定义事务传播行为</li>
                            <li><span class="font-medium">isolation</span> - 设置事务隔离级别</li>
                            <li><span class="font-medium">timeout</span> - 事务超时时间(秒)</li>
                            <li><span class="font-medium">readOnly</span> - 是否为只读事务</li>
                            <li><span class="font-medium">rollbackFor</span> - 触发回滚的异常类型</li>
                        </ul>
                    </div>
                    <div class="bg-green-50 p-4 rounded-lg border border-green-100">
                        <h4 class="font-medium text-green-800 mb-2">典型应用场景</h4>
                        <ul class="text-sm text-gray-700 space-y-2">
                            <li>服务层方法添加事务管理</li>
                            <li>根据业务需求定制事务行为</li>
                            <li>优化只读操作性能</li>
                            <li>处理特殊异常回滚场景</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="mb-12">
                <h3 class="text-xl font-semibold mb-4 flex items-center">
                    <span class="w-6 h-6 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center text-sm mr-3">3.2</span>
                    TransactionInterceptor工作原理
                </h3>
                <p class="text-gray-700 mb-6">TransactionInterceptor是声明式事务管理的核心实现，它拦截带有@Transactional注解的方法调用，在方法执行前后处理事务的启动、提交和回滚。</p>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                    <div class="bg-gray-800 text-white px-4 py-2 flex items-center">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                        <span class="text-sm ml-2">TransactionInterceptor.java</span>
                    </div>
                    <div class="code-block">
                        <pre><code><span class="code-keyword">public class</span> <span class="code-class">TransactionInterceptor</span> <span class="code-keyword">implements</span> <span class="code-class">MethodInterceptor</span> {
    <span class="code-keyword">private final</span> <span class="code-class">PlatformTransactionManager</span> transactionManager;
    <span class="code-keyword">private final</span> <span class="code-class">TransactionAttributeSource</span> transactionAttributeSource;

    <span class="code-keyword">public</span> <span class="code-class">Object</span> <span class="code-method">invoke</span>(<span class="code-class">MethodInvocation</span> invocation) <span class="code-keyword">throws</span> <span class="code-class">Throwable</span> {
        <span class="code-comment">// 获取方法的事务属性</span>
        <span class="code-class">TransactionAttribute</span> txAttr = <span class="code-keyword">this</span>.transactionAttributeSource
            .<span class="code-method">getTransactionAttribute</span>(invocation.<span class="code-method">getMethod</span>(), 
                invocation.<span class="code-method">getThis</span>().<span class="code-method">getClass</span>());
        
        <span class="code-keyword">if</span> (txAttr == <span class="code-keyword">null</span>) {
            <span class="code-keyword">return</span> invocation.<span class="code-method">proceed</span>();  <span class="code-comment">// 无事务属性直接执行</span>
        }
        
        <span class="code-comment">// 获取事务状态</span>
        <span class="code-class">TransactionStatus</span> status = <span class="code-keyword">this</span>.transactionManager.<span class="code-method">getTransaction</span>(txAttr);
        <span class="code-keyword">try</span> {
            <span class="code-class">Object</span> result = invocation.<span class="code-method">proceed</span>();  <span class="code-comment">// 执行目标方法</span>
            <span class="code-keyword">this</span>.transactionManager.<span class="code-method">commit</span>(status);  <span class="code-comment">// 提交事务</span>
            <span class="code-keyword">return</span> result;
        } <span class="code-keyword">catch</span> (<span class="code-class">Throwable</span> ex) {
            <span class="code-keyword">this</span>.transactionManager.<span class="code-method">rollback</span>(status);  <span class="code-comment">// 回滚事务</span>
            <span class="code-keyword">throw</span> ex;
        }
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="mermaid bg-white p-6 rounded-xl shadow-md">
                    sequenceDiagram
                        participant Client
                        participant Proxy
                        participant TransactionInterceptor
                        participant TransactionManager
                        participant TargetObject
                        
                        Client->>Proxy: 调用业务方法
                        Proxy->>TransactionInterceptor: 拦截方法调用
                        TransactionInterceptor->>TransactionManager: getTransaction()
                        TransactionManager-->>TransactionInterceptor: 返回TransactionStatus
                        TransactionInterceptor->>TargetObject: 执行实际方法
                        alt 方法成功执行
                            TransactionInterceptor->>TransactionManager: commit()
                        else 发生异常
                            TransactionInterceptor->>TransactionManager: rollback()
                        end
                        TransactionInterceptor-->>Proxy: 返回结果或异常
                        Proxy-->>Client: 返回结果或异常
                </div>
            </div>
            
            <div>
                <h3 class="text-xl font-semibold mb-4 flex items-center">
                    <span class="w-6 h-6 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center text-sm mr-3">3.3</span>
                    PlatformTransactionManager实现
                </h3>
                <p class="text-gray-700 mb-6">PlatformTransactionManager是Spring中处理事务的核心接口，负责管理事务的生命周期。Spring提供了多个实现类，最常用的是DataSourceTransactionManager和JpaTransactionManager。</p>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                    <div class="bg-gray-800 text-white px-4 py-2 flex items-center">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                        <span class="text-sm ml-2">DataSourceTransactionManager.java</span>
                    </div>
                    <div class="code-block">
                        <pre><code><span class="code-keyword">public class</span> <span class="code-class">DataSourceTransactionManager</span> <span class="code-keyword">implements</span> <span class="code-class">PlatformTransactionManager</span> {
    <span class="code-keyword">private</span> <span class="code-class">DataSource</span> dataSource;

    <span class="code-keyword">@Override</span>
    <span class="code-keyword">public</span> <span class="code-class">TransactionStatus</span> <span class="code-method">getTransaction</span>(<span class="code-class">TransactionDefinition</span> definition) {
        <span class="code-comment">// 获取数据库连接</span>
        <span class="code-class">Connection</span> con = <span class="code-keyword">this</span>.dataSource.<span class="code-method">getConnection</span>();
        <span class="code-comment">// 设置隔离级别</span>
        con.<span class="code-method">setTransactionIsolation</span>(definition.<span class="code-method">getIsolationLevel</span>().<span class="code-method">value</span>());
        <span class="code-comment">// 关闭自动提交</span>
        con.<span class="code-method">setAutoCommit</span>(<span class="code-keyword">false</span>);
        <span class="code-comment">// 设置超时时间</span>
        <span class="code-keyword">if</span> (definition.<span class="code-method">getTimeout</span>() > 0) {
            con.<span class="code-method">setNetworkTimeout</span>(<span class="code-keyword">null</span>, definition.<span class="code-method">getTimeout</span>());
        }
        <span class="code-comment">// 设置只读</span>
        con.<span class="code-method">setReadOnly</span>(definition.<span class="code-method">isReadOnly</span>());
        <span class="code-keyword">return new</span> <span class="code-class">DefaultTransactionStatus</span>(con, <span class="code-keyword">false</span>, <span class="code-keyword">false</span>);
    }

    <span class="code-keyword">@Override</span>
    <span class="code-keyword">public void</span> <span class="code-method">commit</span>(<span class="code-class">TransactionStatus</span> status) {
        <span class="code-class">Connection</span> con = (<span class="code-class">Connection</span>) status.<span class="code-method">getResource</span>();
        con.<span class="code-method">commit</span>();  <span class="code-comment">// 提交事务</span>
    }

    <span class="code-keyword">@Override</span>
    <span class="code-keyword">public void</span> <span class="code-method">rollback</span>(<span class="code-class">TransactionStatus</span> status) {
        <span class="code-class">Connection</span> con = (<span class="code-class">Connection</span>) status.<span class="code-method">getResource</span>();
        con.<span class="code-method">rollback</span>();  <span class="code-comment">// 回滚事务</span>
    }
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Propagation Behavior -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold mr-4">4</div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800">事务传播行为</h2>
            </div>
            
            <p class="text-gray-700 mb-6">Spring事务支持多种传播行为，用于控制事务的传播方式。传播行为定义了当方法在不同事务上下文中被调用时，应该如何处理事务。</p>
            
            <div class="mermaid bg-white p-6 rounded-xl shadow-md mb-8">
                graph LR
                    A[PROPAGATION_REQUIRED] -->|默认值| B(支持当前事务，不存在则创建新事务)
                    C[PROPAGATION_SUPPORTS] --> D(支持当前事务，不存在则以非事务执行)
                    E[PROPAGATION_MANDATORY] --> F(支持当前事务，不存在则抛出异常)
                    G[PROPAGATION_REQUIRES_NEW] --> H(新建事务，暂停当前事务)
                    I[PROPAGATION_NOT_SUPPORTED] --> J(以非事务执行，暂停当前事务)
                    K[PROPAGATION_NEVER] --> L(以非事务执行，存在事务则抛出异常)
                    M[PROPAGATION_NESTED] --> N(嵌套事务，当前事务的子事务)
            </div>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-code-branch text-blue-500 mr-3"></i>
                        REQUIRED(默认值)
                    </h3>
                    <p class="text-gray-600 mb-4">如果当前存在事务，则加入该事务；如果当前没有事务，则创建一个新的事务。</p>
                    <div class="code-block">
                        <pre><code>@Transactional(propagation = Propagation.REQUIRED)
public void methodA() {
    methodB();  // 加入methodA的事务
}

@Transactional(propagation = Propagation.REQUIRED)
public void methodB() {
    // 业务逻辑
}</code></pre>
                    </div>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-code-branch text-purple-500 mr-3"></i>
                        REQUIRES_NEW
                    </h3>
                    <p class="text-gray-600 mb-4">创建一个新的事务，如果当前存在事务，则把当前事务挂起。</p>
                    <div class="code-block">
                        <pre><code>@Transactional(propagation = Propagation.REQUIRED)
public void methodA() {
    methodB();  // 挂起methodA的事务，创建新事务
    // 发生异常不会影响methodB的事务
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void methodB() {
    // 业务逻辑
}</code></pre>
                    </div>
                </div>
            </div>
            
            <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 mb-8">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-exclamation-triangle text-yellow-500 text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <h3 class="text-sm font-medium text-yellow-800">注意</h3>
                        <div class="mt-2 text-sm text-yellow-700">
                            <p>不同传播行为会影响事务的边界和异常处理方式。例如，REQUIRES_NEW创建的新事务与外部事务完全独立，而NESTED则是外部事务的子事务，会受外部事务的影响。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Isolation Level -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold mr-4">5</div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800">事务隔离级别</h2>
            </div>
            
            <p class="text-gray-700 mb-6">Spring支持多种事务隔离级别，用于控制事务之间的可见性。隔离级别越高，数据一致性越强，但并发性能会下降。</p>
            
            <div class="overflow-x-auto bg-white rounded-xl shadow-md mb-8">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">隔离级别</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">脏读</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">不可重复读</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">幻读</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">说明</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">DEFAULT</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">依赖数据库</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">依赖数据库</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">依赖数据库</td>
                            <td class="px-6 py-4 text-sm text-gray-500">使用底层数据库的默认隔离级别</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">READ_UNCOMMITTED</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">可能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">可能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">可能</td>
                            <td class="px-6 py-4 text-sm text-gray-500">最低隔离级别，允许读取未提交数据</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">READ_COMMITTED</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">不可能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">可能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">可能</td>
                            <td class="px-6 py-4 text-sm text-gray-500">只能读取已提交数据(大多数数据库默认级别)</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">REPEATABLE_READ</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">不可能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">不可能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">可能</td>
                            <td class="px-6 py-4 text-sm text-gray-500">确保同一事务中多次读取结果一致</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">SERIALIZABLE</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">不可能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">不可能</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">不可能</td>
                            <td class="px-6 py-4 text-sm text-gray-500">最高隔离级别，完全串行化执行</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-semibold mb-4">隔离问题说明</h3>
                    <ul class="space-y-4">
                        <li>
                            <span class="font-medium">脏读</span>：读取到其他事务未提交的数据
                            <p class="text-sm text-gray-600">事务A修改数据但未提交，事务B读取到未提交的数据，事务A回滚导致事务B读到的是"脏"数据</p>
                        </li>
                        <li>
                            <span class="font-medium">不可重复读</span>：同一事务中多次读取结果不一致
                            <p class="text-sm text-gray-600">事务A读取数据后，事务B修改了该数据并提交，事务A再次读取同一数据得到不同结果</p>
                        </li>
                        <li>
                            <span class="font-medium">幻读</span>：同一事务中多次查询结果集不一致
                            <p class="text-sm text-gray-600">事务A查询符合条件的数据后，事务B插入新数据并提交，事务A再次查询发现多出了数据</p>
                        </li>
                    </ul>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-semibold mb-4">隔离级别设置</h3>
                    <div class="code-block">
                        <pre><code><span class="code-comment">// 设置隔离级别为READ_COMMITTED</span>
@Transactional(isolation = Isolation.READ_COMMITTED)
public void businessMethod() {
    // 业务逻辑
}

<span class="code-comment">// 编程式事务设置隔离级别</span>
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
TransactionStatus status = transactionManager.getTransaction(def);
try {
    // 业务逻辑
    transactionManager.commit(status);
} catch (Exception e) {
    transactionManager.rollback(status);
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Timeout & ReadOnly -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold mr-4">6</div>
                <h2 class="text-2xl md:text-3xl font-bold text-gray-800">事务超时与只读事务</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-xl shadow-md transition-all card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-orange-100 text-orange-600 flex items-center justify-center mr-4">
                            <i class="fas fa-clock"></i>
                        </div>
                        <h3 class="text-xl font-semibold">事务超时</h3>
                    </div>
                    <p class="text-gray-600 mb-4">Spring事务允许设置事务的超时时间(秒)，超时后事务会自动回滚，防止长时间运行的事务占用系统资源。</p>
                    <div class="code-block">
                        <pre><code><span class="code-comment">// 声明式设置超时时间</span>
@Transactional(timeout = 5)
public void longRunningMethod() {
    // 执行时间超过5秒将回滚
}

<span class="code-comment">// 编程式设置超时时间</span>
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setTimeout(5);  <span class="code-comment">// 5秒超时</span>
TransactionStatus status = transactionManager.getTransaction(def);
try {
    // 业务逻辑
    transactionManager.commit(status);
} catch (Exception e) {
    transactionManager.rollback(status);
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md transition-all card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-green-100 text-green-600 flex items-center justify-center mr-4">
                            <i class="fas fa-book-reader"></i>
                        </div>
                        <h3 class="text-xl font-semibold">只读事务</h3>
                    </div>
                    <p class="text-gray-600 mb-4">只读事务(ReadOnly)是一种优化手段，告诉数据库该事务不会修改任何数据，数据库可以针对只读操作进行优化。</p>
                    <div class="code-block">
                        <pre><code><span class="code-comment">// 声明式设置只读事务</span>
@Transactional(readOnly = true)
public List&lt;User&gt; getUserList() {
    // 查询操作
}

<span class="code-comment">// 编程式设置只读事务</span>
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setReadOnly(true);
TransactionStatus status = transactionManager.getTransaction(def);
try {
    // 只读操作
    transactionManager.commit(status);
} catch (Exception e) {
    transactionManager.rollback(status);
}</code></pre>
                    </div>
                    <div class="mt-4 bg-blue-50 p-3 rounded-lg text-sm text-blue-800">
                        <i class="fas fa-info-circle mr-2"></i>
                        只读事务可以提升查询性能，但并不能阻止数据修改操作。某些数据库会根据只读标志进行优化(如MySQL会关闭自动提交)。
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section class="bg-white rounded-xl shadow-md p-8 mb-16">
            <h2 class="text-2xl font-bold text-gray-800 mb-6 text-center">Spring事务管理要点总结</h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="p-4 border-l-4 border-blue-500 bg-blue-50">
                    <h3 class="font-semibold text-lg mb-2">核心组件</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li>PlatformTransactionManager</li>
                        <li>TransactionDefinition</li>
                        <li>TransactionStatus</li>
                        <li>TransactionInterceptor</li>
                    </ul>
                </div>
                <div class="p-4 border-l-4 border-purple-500 bg-purple-50">
                    <h3 class="font-semibold text-lg mb-2">关键概念</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li>传播行为(7种)</li>
                        <li>隔离级别(5种)</li>
                        <li>超时时间</li>
                        <li>只读事务</li>
                    </ul>
                </div>
                <div class="p-4 border-l-4 border-green-500 bg-green-50">
                    <h3 class="font-semibold text-lg mb-2">最佳实践</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li>优先使用声明式事务</li>
                        <li>合理设置事务边界</li>
                        <li>选择合适的隔离级别</li>
                        <li>为长事务设置超时</li>
                    </ul>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h2 class="text-xl font-semibold text-white mb-2">技术小馆</h2>
                    <p class="text-sm">探索技术本质，分享知识价值</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>www.yuque.com/jtostring</span>
                    </a>
                </div>
            </div>
            <div class="mt-8 pt-8 border-t border-gray-800 text-center text-sm">
                <p>© 2023 技术小馆. 版权所有.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```